package thss.mro.util;

public class TypeTransUtil {

	public static byte[] intToBytes(int n) {
		byte[] b = new byte[4];
		for (int i = 0; i < 4; i++) {
			b[i] = (byte) (n >> (24 - i * 8));
		}
		return b;
	}

	public static int bytes2Int(byte[] byteVal) {
		int result = 0;
		byte[] tmp = null;
		if (byteVal.length > 4) {
			int i = 0;
			tmp = new byte[4];
			while (i < 4) {
				tmp[3 - i] = byteVal[byteVal.length - 1 - i];
				i++;
			}
		} else {
			tmp = byteVal;
		}
		for (int i = 0; i < tmp.length; i++) {
			int tmpVal = (tmp[i] << (8 * (tmp.length - 1 - i)));
			result = result | tmpVal;
		}
		return result;
	}

	public static String getBytesString(byte[] b) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < b.length; i++) {
			String hex = Integer.toHexString(b[i] & 0xFF);
			if (hex.length() == 1) {
				hex = '0' + hex;
			}
			sb.append(hex.toUpperCase());
		}
		return sb.toString();
	}
	
}
